package com.forum.controller;


import com.forum.pojo.Admin;
import com.forum.pojo.Audit;
import com.forum.pojo.Message;
import com.forum.pojo.User;
import com.forum.service.AdminService;
import com.forum.service.AuditService;
import com.forum.service.MessageService;
import com.forum.service.UserService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class adminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private MessageService messageService;
    @Autowired
    private AuditService auditService;
    //登录
    @RequestMapping(value = "/login",produces = "text/html;charset=UTF-8")
    public String login(HttpSession session,HttpServletResponse response, Admin admin, Model model){
        if(adminService.login(admin)){
            //登录成功
            session.setAttribute("adminData",admin);
            return "administrator";
        }
        return "administratorlogin";
    }

    //
    @RequestMapping(value = "/unAuditMessage",produces = "text/html;charset=UTF-8")
    public String unAuditMessage( Model model) {
        List<Message> messageList = adminService.findUnAudit();
        model.addAttribute("messageList", messageList);
        for (Message message:messageList){
            System.out.println(message.getMessageID());
        }
        return "workbench";
    }

    @RequestMapping(value = "/AuditTrue",produces = "text/html;charset=UTF-8")
    public String AuditTrue(String messageID,HttpSession session, Model model) {
        adminService.audit(messageID,"通过");
        Admin admin=(Admin) session.getAttribute("adminData");
        auditService.saveAudit(admin.getAdministratorID(),messageID,"通过");
        List<Audit> auditList= auditService.findAudit();
        model.addAttribute("auditList",auditList);
        return "workbench2";
    }

    @RequestMapping(value = "/AuditFalse",produces = "text/html;charset=UTF-8")
    public String AuditFalse(String messageID,HttpSession session, Model model) {
        adminService.audit(messageID,"不通过");
        Admin admin=(Admin) session.getAttribute("adminData");
        auditService.saveAudit(admin.getAdministratorID(),messageID,"不通过");
        List<Audit> auditList= auditService.findAudit();
        model.addAttribute("auditList",auditList);
        return "workbench2";
    }

    @RequestMapping(value = "/deleteMessage",produces = "text/html;charset=UTF-8")
    public String deleteMessage(String messageID, Model model) {
        adminService.deleteMessage(messageID);
        return "workbench2";
    }

    @RequestMapping(value = "/allAudit",produces = "text/html;charset=UTF-8")
    public String allAudit(Model model) {
        List<Audit> auditList = auditService.findAudit();
        model.addAttribute("auditList",auditList);
        return "workbench2";
    }

    @RequestMapping(value = "/outLogin",produces = "text/html;charset=UTF-8")
    public String outLogin(HttpSession session){
        session.invalidate();
        return "index";
    }

}
